// pages/order/order.js
let that,
// QQMapWX = require('../../utils/qqmap-wx-jssdk.js'),
// qqmapsdk,
util = new (require('../../utils/util.js')),
api = api = new(require('../../utils/api.js'));
function gettypename(type){
  let res = '';
  console.log(type,'gettypename');
  
switch (type){
  
  case 'AGENT':{  res = '代理' ;break;   }
  case 'OAAGENT':{  res = '经开代理' ;break;   }
  case 'RECYCLER':{  res = '回收员' ;break;   }
  case 'OARECYCLER':{  res = '经开回收员' ;break;   }
  case 'STOREKEEPER':{  res = '库管员' ;break;   }
  case 'OASTOREKEEPER':{  res = '经开库管员' ;break;   }
}
return res;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    imgurl:[],
    pagesize:15,
    start:0,
    showoa:!0,
    showdis:!0,
    name: '',
    disids:[],
    oadisindex:-1,
    oaids:[],
    phone:'',
    addressdetail: '',
    address:'请选择住址',
    typearr:['回收员','代理','库管员','经济开发区回收员','经济开发区代理','经济开发区库管员'],
    typeindex:1, //0回收员 1代理 2库管员 3经开回收员
    disindex:-1,
    btnloading:!1,
    oaindex:-1,
   
  },

  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this,'this');
    wx.showLoading({
      title: '加载中',
    })
    that = this;
   
    that.getdata()
    
    // qqmapsdk = new QQMapWX({
    //   key: 'CB5BZ-6NP6W-KQGRJ-OENGJ-AD74O-4FFMC' // 必填
    // });
  },
  reset(){
    that.setData({showbtn:!1,loading:!1,list:[]})
    wx.showLoading({
      title: '加载中...',
    })
    that.getdistricts()
  },
 
  getdistricts(){
    wx.showLoading({
      title: '加载中',
    })
    // let city = wx.getStorageSync('city'),disarr = [],disidsarr=[],oadisarr = [],oadisidsarr=[];
    let city = "三门峡",disarr = [],disidsarr=[],oadisarr = [],oadisidsarr=[];
    if(city){
      api.getdistricts(city,res=>{
        wx.hideLoading()
          if(res.data.issuccess==1){
            let {list,useablelist,oalist,oaunlist} = res.data;
           list.forEach((x,i)=>{
              list[i]['checked'] = !1
           })
           if(oalist.length>0){
            oalist.forEach((x,i)=>{
              oalist[i]['checked'] = !1
           })
           }
           if(useablelist.length>0){
            useablelist.forEach((x,i)=>{
              disarr.push(x.DistrictName)
              disidsarr.push(x.ID)
           })
           }
           if(oaunlist.length>0){
            oaunlist.forEach((x,i)=>{
              oadisarr.push(x.name)
              oadisidsarr.push(x.id)
           })
           }

            that.setData({districts:list,disarr,city:city,disidsarr,oalist,oaunlist,oadisarr,oadisidsarr})
           



          }
      })
    }
   
  },
  checkboxChange(e){
    that.setData({disids:e.detail.value,showoa:e.detail.value.length==0})
  },
  checkoaboxChange(e){
   
    that.setData({oaids:e.detail.value,showdis:e.detail.value.length==0})
  },
  getdata(){



    let{pagesize,start} = this.data;
    api.getmyapply({pagesize,start},res=>{
      wx.hideLoading({
        complete: (res) => {},
      })
      if(res.data.issuccess==1){
        if(res.data.list.length>0 ){
          let {list} = res.data // new Date(list[0]['apply']['updateDate'])
            list.forEach((x,i)=>{
              let type = list[i]['apply']['type']; 
            list[i]['applydate'] =  util.formatTimenosecend(new Date(list[i]['date']*1000))
            list[i]['type'] =gettypename(type)
            list[i]['state']=list[i]['apply']['state']=='APPLYING'?'审核中':list[i]['apply']['state']=='SUCCESS'?'审核通过':'审核失败'

          })

          that.setData({list,showbtn:list[0]['apply']['state']=='FAILED'})
        }else{
          that.getdistricts()
        }
      }

    })
  },
  previewimg(e){
    let {src} = e.currentTarget.dataset;
    wx.previewImage({
      urls:[src]
    })
  },
  delimg(e){
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success:res=>{
        if(res.confirm){
          let { index } = e.currentTarget.dataset, { imgurl } = this.data;
          imgurl.splice(index, 1);
          this.setData({imgurl }) 
        }
      }
    })
  },
  addimg(e){
    let { imgurl } = this.data;
    wx.chooseImage({
      count: 2 -imgurl.length,
      sizeType:'compressed',
      success: function(res) {
        imgurl = imgurl.concat(res.tempFilePaths)
        that.setData({ imgurl})

      },
    })
  },

  choseaddress(){
    
    wx.chooseLocation({
     success:res=>{
       let {latitude,longitude} = res;
       qqmapsdk.reverseGeocoder({
        location:`${latitude},${longitude}`,
        success:res1=>{
          console.log('111',res1.result.ad_info);
          // let{province,city,district} = res1.result.ad_info;
          // that.setData({ latitude,longitude,address,province,city,district })
        }
       }
       )
       
       
     },
     complete:(res)=>{
        console.log('complete',res);
        
     }
    })
  },
  switch1Change(e){
    console.log('switch1Change',e);
    
  },
  changeDateTime1(e) {

    this.setData({
      dateTime1: e.detail.value
    });
  },
  bindtypeChange(e){
    console.log(e,'bindtypeChange');
    let {value} = e.detail;
    if(value==0||value==2){
      let {disarr}=that.data;
      if(disarr.length==0) {
        util.nocancelmodal('该地区暂无代理，请先申请代理')
        return;
      }
    }
    if(value==3||value==5){
      let {oadisarr}=that.data;
      if(oadisarr.length==0) {
        util.nocancelmodal('该地区暂无代理，请先申请代理')
        return;
      }
    }
    that.setData({typeindex:e.detail.value})

  },
  binddisChange(e){
    that.setData({disids:[that.data.disarr[e.detail.value]],disindex:e.detail.value})
  },
  binddisoaChange(e){
    that.setData({oadisids:[that.data.oadisarr[e.detail.value]],oadisindex:e.detail.value})

  },
  
 
  input(e){
    that.setData({
      [e.currentTarget.dataset.prop]:e.detail.value
    })
  },
  save(){
    //ts 下单时间戳 毫秒
    that.setData({btnloading:!0})
    let {imgurl,phone,name,city,disids,disindex,typeindex,oaids,oadisindex} = this.data,err= '';
    if(phone.length!=11)err = '电话号码输入有误'
    if(name.length==0)err = '请填写姓名'
    if(typeindex==1&&disids.length==0)err = '请选择代理地区'
    if(typeindex==4&&oaids.length==0)err = '请选择经济开发区代理地区'
    if((typeindex==3||typeindex==5)&&oadisindex<0)err = '请选择经济开发区地区'

    if((typeindex==0||typeindex==2)&&disindex<0)err = '请选择服务地区'
    if(err.length>0){
      that.setData({btnloading:!1})
      wx.showModal({
        title: '提示',
        showCancel:!1,
        content:err
      })
      return;
    }
    //'回收员','代理','库管员','经济开发区回收员','经济开发区代理'
    var type = typeindex==0?'RECYCLER':typeindex==1?'AGENT':typeindex==2?'STOREKEPPER':typeindex==3?'OARECYCLER':typeindex==4?'OAAGENT':'OASTOREKEPPER';
    let data = {phone,name,city:wx.getStorageSync('city')||'',type
    ,districtids:disids.join(','),oaids:String(oaids)}
    if(typeindex==0||typeindex==2){
      data['districtids'] = that.data.disidsarr[disindex]
    }
    if(typeindex==3){
      data['oaids'] = that.data.oadisidsarr[oadisindex]
    }
    if(imgurl.length>0){
      api.uploadimg({ url: 'https://chinawzhs.cn/api/upload/wx_upload', path: imgurl }, {
        success: resarr => {
          wx.hideLoading()
          console.log(resarr,'resarr');
          data['picpath'] = resarr
          that.postdata(data)
          // target['imgurl'] = oldarr.concat(resarr);
          // that.confirmsubmit(target)
        }
      })
    }else{
      util.nocancelmodal('请选择身份证照片')
      
      return;
     // that.postdata(data)

    }

    // 
  },

  postdata(data){
    // util.nocancelmodal('功能逻辑修改中',"",res=>{
    //       })

   
   api.memberapply(data,res=>{
    that.setData({btnloading:!1})
      if(res.data.issuccess==1){
        util.nocancelmodal('申请成功！',"",res=>{
          wx.navigateBack()
          })
      }else{
        util.nocancelmodal(res.data.msg)
      }
   })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})